package com.yu.utils;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.yu.entity.Auth.AuthUserEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @author maxinyu
 * @date 2025-6-4
 */
@Component
public class PasswordUtil {

    @Value("${mall.mgt.password.privateKey:MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA8JH1Wj26wk/cLaph\n" +
            "kIvAukd8V0JUzRUJPDJRkSHvflMSE4sBjPjEg4oOZJhejiZcSlaYCDFvlhzkXp8m\n" +
            "rXbXTQIDAQABAkEAr/2XSFUwc0aJBL9IcL5MV/pD9HIKJ53kC62kDlCNLBOeSUiO\n" +
            "aTBfEwqNybZyeEB+ZItnWyGTTGqNW8dgY0AS4QIhAP46mVlki4VSN9AB+tr4Ci82\n" +
            "D4Zk4bZid0BzgLtk4NT7AiEA8j7/9/FqFZx8/5yk95GQ+GIDFgupsVpGM8D5njAF\n" +
            "glcCIA8b/ncwoZYn38dOBtGywjAyH1QWdM+5JGtBMhpxEWe3AiB3Lj2ay6Aon8Pz\n" +
            "siqLToenW/kuuHA1PDe7sJ5ekDu1owIgFo7lFp+s6yVtsq01O5oH9owyqUUNc+So\n" +
            "JEzIB0S47Is=}")
    private static String privateKey;
    @Autowired
    private PasswordEncoder passwordEncoder;

    private PasswordUtil() {
    }


    public String encode(String password) {
        return passwordEncoder.encode(password);
    }

    public String decodeRsaPassword(AuthUserEntity authUserEntity){
        RSA rsa=new RSA(privateKey,null);
        return new String(rsa.decrypt(authUserEntity.getPassword(), KeyType.PrivateKey));
    }



}